package main

import (
	"fmt"
	"sync"
	"time"
)

// channel知识
var moneyChan = make(chan int)

func shopping(name string, money int, wait *sync.WaitGroup) {
	fmt.Println(name, "来购物")
	time.Sleep(1 * time.Second)
	//将钱放到信道中
	moneyChan <- money
	fmt.Println(name, "购物结束")
	wait.Done()
}

func main() {
	var wait sync.WaitGroup
	var now = time.Now()
	wait.Add(3)
	go shopping("张三", 2, &wait)
	go shopping("里斯", 4, &wait)
	go shopping("王五", 5, &wait)
	go func() {
		defer close(moneyChan)
		wait.Wait()
		//close(moneyChan)
	}()
	var moneyList []int
	for money := range moneyChan {
		//fmt.Println(money)
		moneyList = append(moneyList, money)
	}
	//for {
	//	money, ok := <-moneyChan
	//	fmt.Println(money)
	//	if !ok {
	//		break
	//	}
	//}
	//wait.Wait()
	fmt.Println(time.Since(now))
	fmt.Println(moneyList)
}
